package com.good.msgsys.controller;

import com.good.msgsys.dto.Result;
import com.good.msgsys.dto.ResultCode;
import com.good.msgsys.entity.User;
import com.good.msgsys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 10:18
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 新增用户
    @ApiOperation("新增用户")
    @PostMapping("")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return new Result(ResultCode.SUCCESS, user);
    }

    // 根据id删除用户
    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id){
        userService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    // 更新用户
    @ApiOperation("更新用户")
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") String id, @RequestBody User user){
        user.setId(id);
        userService.update(user);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据id查询用户
    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        User user = userService.findById(id);
        return new Result(ResultCode.SUCCESS, user);
    }

    // 查询所有用户
    @ApiOperation("查询所有用户")
    @GetMapping("")
    public Result findAll() {
        List<User> users = userService.findAll();
        return new Result(ResultCode.SUCCESS, users);
    }
}
